home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: How to make this get_time function work ?
- Date: 5 Mar 1996 22:03 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <5MAR199622034283@erich.triumf.ca>
- References: <4hhjct$3og@idefix.eunet.fi>
- NNTP-Posting-Host: erich.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <4hhjct$3og@idefix.eunet.fi>, mtg@neste.com writes...
- ...
- >int get_time() {
- > time_t t=time(NULL);
- > struct tm *tp=localtime(&t);
- > char time_str[STRING_LENGTH]={0}, str[STRING_LENGTH]={0};
- > strftime(time_str, STRING_LENGTH, "%Y%m%d%H%M%S", tp);
- > sprintf(str,"%s",time_str);
- >/* printf("%s",str); */
- > return str;
- >}
-
-
- >With gcc I get the following error messages:
- >.../test.c: In function `get_time':
- >.../test.c:15: warning: return makes integer from pointer without a cast
-
- Quite reasonable - If you lie to the compiler, it will get it's revenge :-)
- You _said_ that get_time() returns an int, then you try to return a char * -
- what do you expect the compiler to say? Declare the function as
- char *get_time()
- if you want to return a string.
-
- >.../test.c:15: warning: function returns address of local variable
-
- Yes, it does - str[] is a local variable, and goes out of scope when the
- function ends, so you cannot depend on it continuing to exist. You could
- declare it as static - then it will not be destroyed when the function exits,
- and you can safely use a pointer to it outside the function.
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
- or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
-
-